#include <iostream>
#include "inventory.h"

using namespace std;

//method to return the product numbe
int product::getProNum ()
{
	return nProNum;
}
//method to return the quantity
int product::getQuantity ()
{
	return nQuantity;
}
// method to return product name 
void product::getName ()
{
	cout << cname;
}
//method to return the price
float product::getPrice ()
{
	return nPrice;
}
//methof to return any discount
float product::getDiscount ()
{
	return nDiscount;
}
// method which will ask the user to enter the product details
void product::askDetail ( int n )
{
	nProNum = n + 1;//increment the product number by 1
	cout << "==========================" << endl
		<< "Product name==>";//ask for the product name
	cin >> cname;//get the product name
	cout << "Give Quantity==>";//ask for the quantity
	cin >> nQuantity;//get the quantity
	cout << "Give price==>";//ask for the price
	cin >> nPrice;//get the price
	cout << "Give discount==>";//ask if any discount
	cin >> nDiscount;//get the discount
}//ask for details
//mutator function to decrement the number of products id one is deleted
void product::setProNum ()
{
	nProNum--;//decrement if the product is out of stock or deleted 
}
//mutator function to set the details of the product
void product::setDetail ()
{
	cout << "Give Quantity==>";//ask for the quantity
	cin >> nQuantity;//get the quantity
	cout << "Give price==>";//ask for the price
	cin >> nPrice;//get the price
	cout << "Give discount==>";//ask for the discount
	cin >> nDiscount;//get the discount
}

void product::fInput ( ifstream& instream )
{
	instream >> nProNum >> cname >> nQuantity >> nPrice >> nDiscount;
}
//function which will store all the details about the product in a text file
void product::fOutput ()
{
	ofstream outstream;
	outstream.open ( "market_data.txt", ios::app );//open a file
	outstream << nProNum << "\t" << cname << "\t"
		<< nQuantity << "\t" << nPrice << "\t" << nDiscount << endl;//store the details of the product in the file

	outstream.close ();//close the file
}

customer::customer ( void ): nItem ( 0 )
{
	oProduct = new product[nItem];
}

customer::customer ( int n ): nItem ( n )
{
	oProduct = new product[nItem];
}

customer::customer ( const customer& cusObject ): nItem ( cusObject.nItem )
{
	oProduct = new product[nItem];

	for ( int i = 0; i < nItem; i++ )
		oProduct[i] = cusObject.oProduct[i];
}//copy constructor

int customer::getItem ()
{
	return nItem;
}//get item number

void customer::setProduct ()
{
	int n;
	cout << "Edit product number==>";
	cin >> n;
	if ( n <= nItem )
	{
		oProduct[n-1].setDetail ();//change item
		cout << "Item changed" << endl;
	}
	else
		cout << "Item not found!" << endl;
}
//function to add a new product
void customer::addProduct ()
{
	customer objTemp ( *this );//copy to temp object
	int i;

	nItem++;//count item
	oProduct = new product[nItem];//define new object
	for ( i = 0; i < ( nItem - 1 ); i++ )
		oProduct[i] = objTemp.oProduct[i];//copy

	oProduct[i].askDetail ( i );//add
}//add a product
//function to delete a perticular item
void customer::deleteProduct ()
{
	customer objTemp ( *this );//copy to temp object
	nItem--;//count item
	oProduct = new product[nItem];//define new object

	int n, j = 0;
	cout << "Which item to delete==>";
	cin >> n; 
	n--;
	
	for ( int i = 0; i < ( nItem + 1 ); i++ )
	{
		if ( i < n )//if not the item delete
		{
			oProduct[j] = objTemp.oProduct[i];//copy
			j++;
		}
		else if ( i > n )
		{
			oProduct[j] = objTemp.oProduct[i];//copy
			oProduct[j].setProNum ();
			j++;
		}
	}

	cout << "Delete successful!" << endl;//print the message that the delete was successful
}
//function to display the products in the stock.
void customer::showProduct ()
{
	int nSystem;

	cout << "==========================" << endl
		<< "Pro. Num.\tName\tQuantity\tPrice\tDiscount\n";
	if ( nItem == 0 )//check if the list is empty or not
	{
		cout << "No item in the list yet" << endl;//print appropriate message if empty
		nSystem = 4;
	}
	else//if list not empty then print the details of the stock
	{
		for ( int i = 0; i < nItem; i++ )//loop until list ends
		{
			cout 
				<< oProduct[i].getProNum () << "\t\t";//display product number
			oProduct[i].getName ();//display name
			cout << "\t"
				<< oProduct[i].getQuantity () << "\t\t"//display quantity
				<< oProduct[i].getPrice () << "\t"//display price
				<< oProduct[i].getDiscount () << endl;//display discount
		}
		//display the nemu to the user
		cout << "==========================" << endl//
			<< "1.Edit one of the item" << endl//edit one product
			<< "2.Delete an item" << endl//delete one product
			<< "3.Print report" << endl//print a receipt
			<< "4.Back to the main menu" << endl;// back to main menu
		cin >> nSystem;
	}

	switch ( nSystem )
	{
	case 1:
		setProduct ();//set product
		break;
	case 2:
		deleteProduct ();//delete product
		break;
	case 3:
		printreceipt ();//print receipt
		break;
	case 4:
		break;
	default:
		cout << "Number error, back to the main menu..." << endl;//error signal
	}

}//show the item right now
//read a receipt from the file
void customer::readreceipt ()
{
	ifstream instream;
	instream.open ( "market_data.txt" );//open the file to write
	if ( instream.fail () )//if open fail
		cout << "Data file lost, data will be clear" << endl;//print error message
	instream >> nItem;//read item number
	
	oProduct = new product[nItem];//define product size
	for ( int i = 0; i < nItem; i++ )
		oProduct[i].fInput ( instream );//restore data
	instream.close ();//file close
	
	cout << "Restore successful!" << endl;//print restore successful
}
//function to print a receipt
void customer::printreceipt ()
{
	ofstream outstream;
	outstream.open ( "market_data.txt" );//open a file to write
	outstream << nItem << endl;//write the number of items
	outstream.close ();//clear saved data

	float p = calTotal ();//total
	for ( int i = 0; i < nItem; i++ )
		oProduct[i].fOutput ();
	
	outstream.open ( "market_data.txt", ios::app );
	outstream << "Total = " << p;
	outstream.close ();//clear saved data
	cout << "report printed out to file!" << endl;//print the appropriate message
}
//function to calculate the total amount
float customer::calTotal ()
{
	float nTotal = 0;
	for ( int i = 0; i < nItem; i++ )
		nTotal += oProduct[i].getPrice () * oProduct[i].getQuantity () * oProduct[i].getDiscount ();//apply the formula for the total
	//calculate total
	nTotal = nTotal * 1.07;

	return nTotal;//return the total
}

customer::~customer ()//destructor
{
	delete [] oProduct;
}
